package com.webtaglib.tags;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class DateTimeTag extends TagSupport {
	
	private static final long serialVersionUID = 5452252900363999318L;
	private String pattern  = "yyyy-MM-dd HH:mm:ss";
	private Date date;
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	
	public int doEndTag() throws JspTagException{
		SimpleDateFormat sdf=new SimpleDateFormat(pattern);
		if(date==null){
			date=new Date();
		}
		JspWriter out=pageContext.getOut();
		try {
			out.println(sdf.format(date));
		} catch (IOException e) {
			throw new JspTagException(e);
		}
		return EVAL_PAGE;
	}
}
